﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.ComponentModel;

namespace Common
{
    /// 枚举帮助类
    /// <summary>
    /// 枚举帮助类
    /// </summary>
    public class EnumHelper
    {
        private static readonly Object lockObj = new object();
        private static Dictionary<Type, SortedList<int, string>> m_enums = new Dictionary<Type, SortedList<int, string>>();


        #region 通用方法

        /// 根据枚举类型得到所有的枚举值
        /// <summary>
        /// 根据枚举类型得到所有的枚举值
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static SortedList<int, string> GetEnums(Type t)
        {
            if (!m_enums.Keys.Contains(t))
            {
                lock (lockObj)
                {
                    if (!m_enums.Keys.Contains(t))
                    {
                        SortedList<int, string> list = new SortedList<int, string>();

                        Array a = Enum.GetValues(t);
                        for (int i = 0; i < a.Length; i++)
                        {
                            string enumName = a.GetValue(i).ToString();
                            int enumKey = (int)Enum.Parse(t, enumName);

                            string enumDescription = GetDescription(t, enumKey);
                            list.Add(enumKey, enumDescription);
                        }

                        m_enums.Add(t, list);
                    }
                }
            }

            if (m_enums.Keys.Contains(t))
                return m_enums[t];

            return new SortedList<int, string>();
        }


        /// 获取enum定义描述
        /// <summary>
        /// 获取enum定义描述
        /// </summary>
        /// <param name="t"></param>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetEnumDesc(Type t, int enumValue)
        {
            var enums = GetEnums(t);
            if (enums.Keys.Contains(enumValue))
                return enums[enumValue];

            return string.Empty;
        }

        /// 枚举转字典集合  
        /// <summary>  
        /// 枚举转字典集合  
        /// </summary>  
        /// <typeparam name="T">枚举类名称</typeparam>  
        /// <param name="keyDefault">默认key值</param>  
        /// <param name="valueDefault">默认value值</param>  
        /// <returns>返回生成的字典集合</returns>  
        public static Dictionary<object, string> EnumListDic<T>(string keyDefault, string valueDefault = "")
        {
            Dictionary<object, string> dicEnum = new Dictionary<object, string>();
            Type enumType = typeof(T);
            if (!enumType.IsEnum)
            {
                return dicEnum;
            }
            if (!string.IsNullOrEmpty(keyDefault)) //判断是否添加默认选项  
            {
                dicEnum.Add(keyDefault, valueDefault);
            }
            string[] fieldstrs = Enum.GetNames(enumType); //获取枚举字段数组  
            foreach (var item in fieldstrs)
            {
                string description = string.Empty;
                var field = enumType.GetField(item);
                object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //获取属性字段数组  
                if (arr != null && arr.Length > 0)
                {
                    description = ((DescriptionAttribute)arr[0]).Description;   //属性描述  
                }
                else
                {
                    description = item;  //描述不存在取字段名称  
                }
                //dicEnum.Add(description, (int)Enum.Parse(enumType, item));
                dicEnum.Add((int)Enum.Parse(enumType, item), description);  //不用枚举的value值作为字典key值的原因从枚举例子能看出来，其实这边应该判断他的值不存在，默认取字段名称  
            }
            return dicEnum;
        }

        /// 根据枚举值获取枚举key
        /// <summary>
        /// 根据枚举值获取枚举key
        /// </summary>
        /// <param name="t"></param>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetEnumName(Type t, int enumValue)
        {
            return Enum.GetName(t, enumValue);
        }

        #endregion

        #region private

        #region 根据枚举类型及值得到对应的名字
        /// <summary>
        /// 根据枚举类型及值得到对应的名字
        /// </summary>
        /// <param name="t"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        private static string GetName(Type t, object v)
        {
            try
            {
                return Enum.GetName(t, v);
            }
            catch
            {
                return "UNKNOWN";
            }
        }
        #endregion

        #region 返回指定枚举类型的指定值的描述
        /// <summary>
        /// 返回指定枚举类型的指定值的描述
        /// </summary>
        /// <param name="t">枚举类型</param>
        /// <param name="v">枚举值</param>
        /// <returns></returns>
        public static string GetDescription(Type t, object v)
        {
            try
            {
                FieldInfo fi = t.GetField(GetName(t, v));
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
            }
            catch
            {
                return "UNKNOWN";
            }
        }
        #endregion

        #endregion
    }
}
